#include "Board.h"

CBoard::CBoard():NumFiles(0),NumColumns(0)
{

}

CBoard::~CBoard()
{

}

void CBoard::Destroy()
{
	VectorCells.DeleteListNodes();
}

vPos CBoard::GetBoardXPosition(u32 XPos)
{
    return (vPos)(-1.0 + (((2.0 / NumColumns)*XPos) + NumFiles / (vPos)(NumFiles * NumColumns)));
}

vPos CBoard::GetBoardYPosition(u32 YPos)
{
    return (vPos)(1.0 - (((2.0 / NumFiles)*YPos) + NumColumns / (vPos)(NumFiles*NumColumns)));
}

vector2dvpos CBoard::GetBoardPosition(vector2dvpos Pos)
{
    return vector2dvpos(GetBoardPosition(Pos.X,Pos.Y));
}

vector2dvpos CBoard::GetBoardPosition(vPos XPos,vPos YPos)
{
    return vector2dvpos(GetBoardXPosition((u32)XPos),GetBoardYPosition((u32)YPos));
}

void CBoard::SetSizeBoardCell(I2DNode *node)
{
    node->SetLenPropRelative((vPos)(100.0 / NumFiles),(vPos)(100.0 / NumColumns));
}

void CBoard::SetPosBoardCell(I2DNode *node,u32 PosX,u32 PosY)
{
    vPos xpos = GetBoardXPosition(PosX);
    vPos ypos = GetBoardYPosition(PosY);
    node->GetSpriteNode()->SetPositionXY(xpos,ypos);
}

void CBoard::SetBoardSize()
{
    s32 files = ConfigManager->GetVarAsInt(Level,"Files");
    s32 columns = ConfigManager->GetVarAsInt(Level,"Columns");

    NumFiles = files;
    NumColumns = columns;

}

s32 CBoard::LoadBoard()
{
    MoveQuad.Top.X = ConfigManager->GetVarAsInt(Level,"eTopX");
    MoveQuad.Top.Y = ConfigManager->GetVarAsInt(Level,"eTopY");
    MoveQuad.Down.X = ConfigManager->GetVarAsInt(Level,"eDownX");
    MoveQuad.Down.Y = ConfigManager->GetVarAsInt(Level,"eDownY");

    for (u32 y=0;y<NumFiles;y++)
    {
        for (u32 x=0;x<NumColumns;x++)
        {
            u32 Index = (y*NumColumns) + x;

            I2DNode *Temp = new I2DNode();

			CColor c;

            if (y%2)
            {
                if (x%2)
					c.SetRGB(1.0,1.0,0.0);
                else
					c.SetRGB(0.0,1.0,1.0);
            }
            else
            {
                if (x%2)
					c.SetRGB(0.5,0.0,0.5);
                else
   					c.SetRGB(1.0,0.0,0.0);
            }

			if (MoveQuad.IsPointInside(x,y))
			{
				c.MultScalar(0.5);
			}

			Temp->CreateEmptyNode(c);

            SetSizeBoardCell(Temp);
            SetPosBoardCell(Temp,x,y);
            Temp->GetSpriteNode()->SetPositionZ((vPos)BOARD_POS_Z);
			VectorCells.push_back(Temp);
        }
    }

	return 0;
}

void CBoard::SetPosFicha(CFicha *Ficha,u32 x,u32 y)
{
    SetPosBoardCell(Ficha->GetFichaGraph(),x,y);
    Ficha->SetFichaPos(x,y);
}

void CBoard::SetFichaOnTop(CFicha *Ficha)
{
    SetPosFicha(Ficha,MoveQuad.Top.X,MoveQuad.Top.Y);
}

void CBoard::SetLevel(stringc level)
{
    Level = level;
}

void CBoard::PutNewEnemy(CEnemy *newenemy)
{
}
